package com.spaceobj.chess.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spaceobj.chess.common.Result;
import com.spaceobj.chess.entity.MatchInfo;
import com.spaceobj.chess.service.MatchInfoService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "对局信息管理", description = "对局信息相关接口")
@RestController
@RequestMapping("/api/match-info")
@RequiredArgsConstructor
public class MatchInfoController {

    private final MatchInfoService matchInfoService;

    @Operation(summary = "创建对局信息")
    @PostMapping
    public Result<MatchInfo> createMatchInfo(@RequestBody MatchInfo matchInfo) {
        matchInfoService.save(matchInfo);
        return Result.success(matchInfo);
    }

    @Operation(summary = "更新对局信息")
    @PutMapping("/{id}")
    public Result<MatchInfo> updateMatchInfo(
            @Parameter(description = "对局ID") @PathVariable Integer id,
            @RequestBody MatchInfo matchInfo) {
        matchInfo.setMatchId(id);
        matchInfoService.updateById(matchInfo);
        return Result.success(matchInfo);
    }

    @Operation(summary = "删除对局信息")
    @DeleteMapping("/{id}")
    public Result<Void> deleteMatchInfo(@Parameter(description = "对局ID") @PathVariable Integer id) {
        matchInfoService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "获取对局详情")
    @GetMapping("/{id}")
    public Result<MatchInfo> getMatchInfo(@Parameter(description = "对局ID") @PathVariable Integer id) {
        return Result.success(matchInfoService.getById(id));
    }

    @Operation(summary = "分页查询对局信息")
    @GetMapping
    public Result<Page<MatchInfo>> listMatchInfo(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "对局状态") @RequestParam(required = false) Integer status) {
        Page<MatchInfo> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<MatchInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (status != null) {
            queryWrapper.eq(MatchInfo::getMatchMode, status);
        }
        return Result.success(matchInfoService.page(pageParam, queryWrapper));
    }
} 